﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Windows.Forms
{

    using System;
    using System.Drawing;
    using System.Diagnostics.CodeAnalysis;
    using System.Windows.Forms.VisualStyles;
    using Microsoft.Win32;

    /// <summary>
    ///    <para>
    ///       This is a rendering class for the ProgressBar control.
    ///    </para>
    /// </summary>
    public sealed class ProgressBarRenderer
    {

        //Make this per-thread, so that different threads can safely use these methods.
        [ThreadStatic]
        private static VisualStyleRenderer visualStyleRenderer = null;

        //cannot instantiate
        private ProgressBarRenderer()
        {
        }

        /// <summary>
        ///    <para>
        ///       Returns true if this class is supported for the current OS and user/application settings, 
        ///       otherwise returns false.
        ///    </para>
        /// </summary>
        public static bool IsSupported
        {
            get
            {
                return VisualStyleRenderer.IsSupported; // no downlevel support
            }
        }

        /// <summary>
        ///    <para>
        ///       Renders a horizontal bar.
        ///    </para>
        /// </summary>
        [
            SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters") // Using Graphics instead of IDeviceContext intentionally
        ]
        public static void DrawHorizontalBar(Graphics g, Rectangle bounds)
        {
            InitializeRenderer(VisualStyleElement.ProgressBar.Bar.Normal);

            visualStyleRenderer.DrawBackground(g, bounds);
        }

        /// <summary>
        ///    <para>
        ///       Renders a vertical bar.
        ///    </para>
        /// </summary>
        [
            SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters") // Using Graphics instead of IDeviceContext intentionally
        ]
        public static void DrawVerticalBar(Graphics g, Rectangle bounds)
        {
            InitializeRenderer(VisualStyleElement.ProgressBar.BarVertical.Normal);

            visualStyleRenderer.DrawBackground(g, bounds);
        }

        /// <summary>
        ///    <para>
        ///       Renders a number of constant size horizontal chunks in the given bounds.
        ///    </para>
        /// </summary>
        [
            SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters") // Using Graphics instead of IDeviceContext intentionally
        ]
        public static void DrawHorizontalChunks(Graphics g, Rectangle bounds)
        {
            InitializeRenderer(VisualStyleElement.ProgressBar.Chunk.Normal);

            visualStyleRenderer.DrawBackground(g, bounds);
        }

        /// <summary>
        ///    <para>
        ///       Renders a number of constant size vertical chunks in the given bounds.
        ///    </para>
        /// </summary>
        [
            SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters") // Using Graphics instead of IDeviceContext intentionally
        ]
        public static void DrawVerticalChunks(Graphics g, Rectangle bounds)
        {
            InitializeRenderer(VisualStyleElement.ProgressBar.ChunkVertical.Normal);

            visualStyleRenderer.DrawBackground(g, bounds);
        }

        /// <summary>
        ///    <para>
        ///       Returns the  width/height of a single horizontal/vertical progress bar chunk.
        ///    </para>
        /// </summary>
        public static int ChunkThickness
        {
            get
            {
                InitializeRenderer(VisualStyleElement.ProgressBar.Chunk.Normal);

                return (visualStyleRenderer.GetInteger(IntegerProperty.ProgressChunkSize));
            }
        }

        /// <summary>
        ///    <para>
        ///       Returns the  width/height of the space between horizontal/vertical progress bar chunks.
        ///    </para>
        /// </summary>
        public static int ChunkSpaceThickness
        {
            get
            {
                InitializeRenderer(VisualStyleElement.ProgressBar.Chunk.Normal);

                return (visualStyleRenderer.GetInteger(IntegerProperty.ProgressSpaceSize));
            }
        }

        private static void InitializeRenderer(VisualStyleElement element)
        {
            if (visualStyleRenderer == null)
            {
                visualStyleRenderer = new VisualStyleRenderer(element);
            }
            else
            {
                visualStyleRenderer.SetParameters(element);
            }
        }
    }
}
